/**
 * 
 */
package leetCode;

import java.util.ArrayList;

/**
 * @author zhong
 *
 */
public class ReorderLogFiles {
	public String[] reorderLogFiles(String[] logs) {
		ArrayList<String> letter_logs = new ArrayList<>();
		ArrayList<String> digit_logs = new ArrayList<>();
		for (String log : logs) {
			if (isDigitLog(log)) {
				digit_logs.add(log);
			} else {
				letter_logs.add(log);
			}
		}
		letter_logs.sort((a, b) -> {
			return a.substring(getPos(a)).compareTo(b.substring(getPos(b)));
		});
		String[] reStrings = new String[logs.length];
		int i = 0;
		for (; i < letter_logs.size(); i++) {
			reStrings[i] = letter_logs.get(i);
		}
		int j = 0;
		for (; i < reStrings.length; i++, j++) {
			reStrings[i] = digit_logs.get(j);
		}
		return reStrings;
	}

	private boolean isDigitLog(String log) {
		String sub = log.substring(getPos(log));
		for (int i = 0; i < sub.length(); i++) {
			char ch = sub.charAt(i);
			if (ch != ' ' && !Character.isDigit(ch))
				return false;
		}
		return true;
	}

	private int getPos(String a) {
		int posA = a.indexOf(" ");
		return posA + 1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] Input = { "a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo" };
		Input = new ReorderLogFiles().reorderLogFiles(Input);
		for (String string : Input) {
			System.out.println(string);
		}
	}

}
